package blog.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import blog.entity.User;

//一个类继承了HandlerInterceptorAdapter类或是实现了HandlerInterceptor接口就成为了一个拦截器
public class CheckLoginInterceptor extends HandlerInterceptorAdapter {
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("session_user");
		if (user == null) {
			String contextpath = request.getServletContext().getContextPath();
			// 没有登录则不放行,并且回到登录界面
			response.sendRedirect(contextpath + "/user/login");
			return false;
		}

		return true; // 已登录则放行
	}
}
